package com.guang.springmybatis.scan;

import com.guang.springmybatis.beanfactory.LGBeanFactory;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.AnnotationMetadata;

import java.io.IOException;
import java.util.Objects;
import java.util.Set;

/**
 *
 * 重写一个扫描规则
 *
 * @Description
 * @Author liguang
 * @Date 2022/08/23/21:44
 */
public class LGClassPathMapperScanner extends ClassPathBeanDefinitionScanner {

    public LGClassPathMapperScanner(BeanDefinitionRegistry registry) {
        super(registry);
    }

    /**
     * 判断是否是一个接口即可
     * @param beanDefinition
     * @return
     */
    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        AnnotationMetadata metadata = beanDefinition.getMetadata();
        return metadata.isIndependent() && metadata.isInterface();
    }

    @Override
    protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);
        //        // 获取得到所有的beanDefinitionHolders
//        Set<BeanDefinitionHolder> beanDefinitionHolders = lgClassPathMapperScanner.doScan(mapperInterfacePath);
//        // 得到之后需要进行修改
        for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
            BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
            // 这里设置的是实际的值
            beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(Objects.requireNonNull(beanDefinition.getBeanClassName()));
            // 修改成为我们自己的东西来定义LGBeanFactoryProcessor
            beanDefinition.setBeanClassName(LGBeanFactory.class.getName());
        }
        for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
            System.out.println(beanDefinitionHolder.getBeanDefinition());
        }
        return beanDefinitionHolders;
    }
}
